/*
 * TemplateFactory.h
 *
 *  Created on: 15.07.2011
 *      Author: sili
 */

#ifndef TEMPLATEFACTORY_H_
#define TEMPLATEFACTORY_H_

#include "Template.h"
#include <Magick++.h>

using namespace Magick;

class TemplateFactory : public ActionListener, public cThread{
private:
	string configDir;				// The config directory where the templates.xml files is stored.
	string templateXmlFile;			// The system.xml file inside the configuration directory
	Controller * controller;		// Controller. Used e.g. to publishing a log action
	map<string,Template *> templates;	//A map of all defined templates. (which are stored and restored form the templates.xml - file)
	vector<AALAction *> templateActions;	// A queue of actions. e.g. doTmplate - actions.

protected:
	/**
	 * cThread implementation. Execute this in a separate thread.
	 * doTemplate - actions will be done in a separate thread.
	 */
	virtual void Action(void);

public:
	TemplateFactory(Controller * controller, const char * _configDir);
	virtual ~TemplateFactory();


	/**
	 * OVERWRITE
	 * I'm an ActionListener
	 */
	void doAction(AALAction * _action);

	/**
	 * Nothing to do here.
	 * This class doesn't need to listen to the main thread loop of the vdr-plugin.
	 */
	void doMainSavedAction();

	/*
	 * Creates an template instance from SVG DOMDocument.
	 * Do it only if it wasn't already created in this session.
	 * Use Inkscape to create an PNG image of this SVD DOMDocument-Node. Place it inside template directory.
	 * Create also an Thumbnail which is shown in the Admin Frontend
	 */
	void createTemplate(DOMDocument * _doc, DOMNode * _selfSvgNode, const char * _svgFile, const map<string, AALArea> & _svgAreas, time_t _templateCreation);

	/**
	 * Set the visibility to true/false fo this SVG DOMDocument.
	 * Usefull to hide all AALItem - childelements inside an node to be able to create
	 * the parent node as an AALItem - template.
	 */
	void setVisibility(DOMElement * _selfSvgNode, const bool _visible);

	/**
	 * Search the internal map for the given template name.
	 * If found copy the PNG file as an Magic-Instance into the given _copyInto reference.
	 */
	void getTemplate(const char * _template, Image & _copyInto);

	/**
	 * save the internal template map as template.xml file
	 */
	void serializeTemplatesFile();

	/*
	 * restore the internal template map from template.xml file
	 */
	void deSerializeTemplatesFile();

	/*
	 * Return the Template instance for the giving _templateName.
	 * Return NULL if not found
	 */
	Template * getTemplate(const char * _templateName);

	/*
	 * Create a new one or return existing Template instance for the giving _templateName.
	 */
	Template * createOrGetExistingTemplate(const char * _catName);

	/*
	 * Deletes the template with the giving _templateName.
	 * Response e.g. ok/fail is saved in the _response reference.
	 */
	bool deleteTemplate(const char * _templateName, string * _response);

	/**
	 * Saves an flatted list of all available templates into the _list reference.
	 *
	 */
	void getFlatList(list<Template *> * _list, int _currentLevel);

	/**
	 * Updated an template. Reason may be e.g. a renamed template.
	 * Deletes the old PNG & thumbnail image and saves it under the new name.
	 * Response e.g. ok/fail is stored in the _resopnse reference.
	 */
	bool adoptTemplate(Template * _template, const char * _oldName, string * _response);

	/*
	 * Lists all available templates into the _fillInto list as JSON - object.
	 */
	void getTemplateImagesAsJSON(string * _fillInto);

};

#endif /* TEMPLATEFACTORY_H_ */
